//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Interfaces;

namespace Eas.WebCrawler.Test.Scenario {
    public abstract class EngineAction : NMock2.IAction {

        protected Eas.WebCrawler.Interfaces.WebCrawlerEngine _Engine;
        protected bool executed = false;
        public EngineAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) {
            _Engine = engine;
        }

        #region IInvokable Members

        public void Invoke(NMock2.Monitoring.Invocation invocation) {
            if (!executed) {
                executed = true;
                Execute();
            }
        }

        protected abstract void Execute();

        #endregion

        #region ISelfDescribing Members

        public void DescribeTo(System.IO.TextWriter writer) {
            writer.WriteLine("Engine Action");
        }

        #endregion
    }

    public class PauseAndUnPauseAction : NMock2.IAction {
        protected Eas.WebCrawler.Interfaces.WebCrawlerEngine _Engine;
        int executeCount;
        public PauseAndUnPauseAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine, bool executePause) {
            _Engine = engine;
            executeCount = executePause ? 0 : 2;
        }

        #region IInvokable Members

        public void Invoke(NMock2.Monitoring.Invocation invocation) {
            ProcessingStateMessageEventArgs args = (ProcessingStateMessageEventArgs)invocation.Parameters[1];
            string text = args.Message;
            if (text.IndexOf("Transitioned from") >= 0) {
                switch (executeCount) {
                    case 0:
                        ++executeCount;
                        _Engine.Pause();
                        break;

                    case 1:
                        ++executeCount;
                        _Engine.Start();
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion

        #region ISelfDescribing Members

        public void DescribeTo(System.IO.TextWriter writer) {
            writer.Write(" PauseAndUnpause ");
        }

        #endregion
    }

    public class PauseAction : EngineAction {

        public PauseAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) : base(engine) { }

        protected override void Execute() {
            _Engine.Pause();
        }
    }

    public class UnPauseAction : EngineAction {
        public UnPauseAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) : base(engine) { }

        protected override void Execute() {
            _Engine.Start();
        }
    }

    public class StopAction : EngineAction {
        public StopAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) : base(engine) { }

        protected override void Execute() {
            _Engine.Stop();
        }
    }

    public class StartAction : EngineAction {
        public StartAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) : base(engine) { }

        protected override void Execute() {
            _Engine.Start();
        }
    }

    public class SetFailOnBeginAction : EngineAction {
        public SetFailOnBeginAction(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) : base(engine) { }

        protected override void Execute() {
            TestGateway._ThrowFor = ThrowFor.BeginGetResponse;
        }
    }
}
